package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.enums.SaleOrPurchType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.numGene.NumberGene;
import com.ruoyi.system.domain.AccountDetailss;
import com.ruoyi.system.service.IAccountDetailssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FundsMapper;
import com.ruoyi.system.domain.Funds;
import com.ruoyi.system.service.IFundsService;

/**
 * 账户管理Service业务层处理
 * 
 * @author he
 * @date 2025-10-20
 */
@Service
public class FundsServiceImpl implements IFundsService 
{
    @Autowired
    private FundsMapper fundsMapper;

    @Autowired
    private IAccountDetailssService accountDetailssService;

    /**
     * 查询账户管理
     * 
     * @param id 账户管理主键
     * @return 账户管理
     */
    @Override
    public Funds selectFundsById(Long id)
    {
        return fundsMapper.selectFundsById(id);
    }

    /**
     * 查询账户管理列表
     * 
     * @param funds 账户管理
     * @return 账户管理
     */
    @Override
    public List<Funds> selectFundsList(Funds funds)
    {
        return fundsMapper.selectFundsList(funds);
    }

    /**
     * 新增账户管理
     * 
     * @param funds 账户管理
     * @return 结果
     */
    @Override
    public int insertFunds(Funds funds)
    {
        funds.setCreateTime(DateUtils.getNowDate());
        funds.setCreateUserId(SecurityUtils.getUserId());
        funds.setCreateUserName(SecurityUtils.getUsername());

        //账户详情
        AccountDetailss accountDetailss = new AccountDetailss();
        accountDetailss.setAmountId(funds.getId());
        accountDetailss.setTransactionNumber(NumberGene.generateCode("CSK"));
        accountDetailss.setTransactionAmountBefore(BigDecimal.ZERO);
        accountDetailss.setTransactionAmount(funds.getResidueAmount());
        accountDetailss.setTransactionType(SaleOrPurchType.FIRST_TYPE.getMessage());
        accountDetailss.setResidueAmount(BigDecimal.ZERO.add(funds.getResidueAmount()));
        accountDetailss.setAmountName(funds.getName());

        accountDetailssService.insertAccountDetailss(accountDetailss);

        return fundsMapper.insertFunds(funds);
    }

    /**
     * 修改账户管理
     * 
     * @param funds 账户管理
     * @return 结果
     */
    @Override
    public int updateFunds(Funds funds)
    {
        funds.setUpdateTime(DateUtils.getNowDate());
    funds.setUpdateUserId(SecurityUtils.getUserId());
    funds.setUpdateUserName(SecurityUtils.getUsername());
        return fundsMapper.updateFunds(funds);
    }

    /**
     * 批量删除账户管理
     * 
     * @param ids 需要删除的账户管理主键
     * @return 结果
     */
    @Override
    public int deleteFundsByIds(Long[] ids)
    {
        return fundsMapper.deleteFundsByIds(ids);
    }

    /**
     * 删除账户管理信息
     * 
     * @param id 账户管理主键
     * @return 结果
     */
    @Override
    public int deleteFundsById(Long id)
    {
        return fundsMapper.deleteFundsById(id);
    }

    @Override
    public int updateApprovalState(List<Long> ids, Integer approvalState) {
        return fundsMapper.updateApprovalState(ids, approvalState);
    }
}
